clear;

%%%%%%%%%%%%%%%%%%%%%%Data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data = dataset('File','table2_data.csv','Delimiter',',');

iter = 1;
N = 615;
warning('off','all');
stations = [data.station_id1, data.station_id2, data.station_id3];

y1 = data.voteshare_upa_2014;
X1 = [ones(N,1) data.voteshare_upa_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y2 = data.voteshare_statecm_2014;
X2 = [ones(N,1) data.voteshare_statecm_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y3 = data.voteshare_inc_2014;
X3 = [ones(N,1) data.voteshare_inc_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y4 = data.voteshare_aap_2014;
X4 = [ones(N,1) data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y6 = data.voteshare_crime_2014;
X6 = [ones(N,1) data.voteshare_crime_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y7 = data.voteshare_rich_2014;
X7 = [ones(N,1) data.voteshare_rich_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y8 = data.voteshare_events_2014;
X8 = [ones(N,1) data.voteshare_events_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y9 = data.voteshare_volunteers_2014;
X9 = [ones(N,1) data.voteshare_volunteers_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y11 = data.voteshare_spend_2014;
X11 = [ones(N,1) data.voteshare_spend_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

y12 = data.voteshare_win_2014;
X12 = [ones(N,1) data.voteshare_win_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

idx = find(~isnan(y1));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y1(idx),data.treat(idx),X1(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y1(idx),X1(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,1) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y2));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y2(idx),data.treat(idx),X2(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y2(idx),X2(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,2) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y3));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y3(idx),data.treat(idx),X3(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y3(idx),X3(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,3) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y4));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y4(idx),data.treat(idx),X4(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y4(idx),X4(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,4) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

%idx = find(~isnan(y5));
%[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y5(idx),data.treat(idx),X5(idx,:),data.wgt_treat(idx),stations(idx,:),2);
%p_ri = ri(iter,beta,se,y5(idx),X5(idx,:),stations(idx,:),2,data.prob_treat(idx));
%results(:,5) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y6));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y6(idx),data.treat(idx),X6(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y6(idx),X6(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,5) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y7));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y7(idx),data.treat(idx),X7(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y7(idx),X7(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,6) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y8));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y8(idx),data.treat(idx),X8(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y8(idx),X8(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,7) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y9));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y9(idx),data.treat(idx),X9(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y9(idx),X9(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,8) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

%idx = find(~isnan(y10));
%[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y10(idx),data.treat(idx),X10(idx,:),data.wgt_treat(idx),stations(idx,:),2);
%p_ri = ri(iter,beta,se,y10(idx),X10(idx,:),stations(idx,:),2,data.prob_treat(idx));
%results(:,9) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y11));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y11(idx),data.treat(idx),X11(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y11(idx),X11(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,9) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

idx = find(~isnan(y12));
[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y12(idx),data.treat(idx),X12(idx,:),data.wgt_treat(idx),stations(idx,:),2);
p_ri = ri(iter,beta,se,y12(idx),X12(idx,:),stations(idx,:),2,data.prob_treat(idx));
results(:,10) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

format bank;
disp(results);


